//
//  XTMainTabBar.swift
//  HrenryWeibo
//
//  Created by 熊涛 on 16/5/10.
//  Copyright © 2016年 熊涛. All rights reserved.
//

import UIKit

class XTMainTabBar: UITabBar {

    //MARK: - 属性
    //闭包() -> () 需要用括号括起来  不然就不知道是是什么东西
    var btnCallBack: (() -> ())?
    
    
    
    //MARK: - layoutSubviews
    override func layoutSubviews() {
        
        super.layoutSubviews()
        
        /**
        
        <UITabBarButton: 0x7f812b4c85b0; frame = (2 1; 90 48); //首页
        <UITabBarButton: 0x7f812b649ed0; frame = (96 1; 90 48); //消息
        <UITabBarButton: 0x7f812b64e240; frame = (190 1; 89 48); // 发现
        <UITabBarButton: 0x7f812b6524a0; frame = (283 1; 90 48); // 我
        <UIButton: 0x7f812b520ea0; frame = (0 0; 100 49); // 加号按钮
        
        */
        
        //计算子控件的宽度
        let width  = self.bounds.width / 5
        //记录按钮的位置
        var index = 0
//        XTPrint(items: "index1111 : \(index)")
//        XTPrint(items: "UITabBarButton : \(self.subviews)")
        for sub in subviews {
            
            
            if sub.isKindOfClass(NSClassFromString("UITabBarButton")!) {
                
                sub.frame = CGRect(x: CGFloat(index) * width, y: 0, width: width, height: self.frame.height)
//                XTPrint(items: "index : \(index)")
//                XTPrint(items: "frame : \(sub.frame)")
                index++
            }
            
            if index == 2 {
                btn.frame = CGRect(x: CGFloat(index) * width, y: 0, width: width, height: self.frame.height)
                index++
                
            }
            
            
        }
        
    }
    
    
    // MARK: - 点击事件
    func btnClickAction () {
    
        btnCallBack?()
    }
    
    //MARK: - 懒加载
    
    lazy var btn : UIButton = {
        
        //添加加号按钮
        let btn = UIButton(type: UIButtonType.Custom)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        btn.setBackgroundImage(UIImage(named:"tabbar_compose_button"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        //添加点击事件
        btn.addTarget(self, action: "btnClickAction", forControlEvents: UIControlEvents.TouchUpInside)
        
        self.addSubview(btn)
        
        
        
        return btn
    }()
    
}
